package com.huopaolan.yohuoshop.Mvp.Presenter;

import com.huopaolan.lib_core.Mvp.Presenter.BasePresenter;
import com.huopaolan.yohuoshop.Bean.BaseEntity;
import com.huopaolan.yohuoshop.Bean.UserEntity;
import com.huopaolan.yohuoshop.Bean.UserMessageEntity;
import com.huopaolan.yohuoshop.Mvp.Contract.UserContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class UserDetailsPresenter extends BasePresenter<UserContract.UserDetailsIView, UserContract.UserDetailsIModel> {
    @Inject
    public UserDetailsPresenter(UserContract.UserDetailsIView userDetailsIView, UserContract.UserDetailsIModel userDetailsIModel) {
        super(userDetailsIView, userDetailsIModel);
    }

    public void SelUserRequest(String userid) {
        JSONObject sel = new JSONObject();
        try {
            sel.put("userid", userid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String request = sel.toString();
        m.sel_user(request).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserMessageEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserMessageEntity entity) {
                        if (entity == null) {
                            return;
                        } else {
                            v.SelUserDetailsSuccess(entity);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void UpdateUserRequest(String userid, String username, String usersex, String userbirthday, String userheight, String userweight) {
        JSONObject update = new JSONObject();
        try {
            update.put("userid", userid);
            update.put("nickname", username);
            update.put("usersex", usersex);
            update.put("userbirthday", userbirthday);
            update.put("userheight", userheight);
            update.put("userweight", userweight);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String request = update.toString();
        m.user_details(request).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseEntity userEntity) {
                        if (userEntity == null) {
                            return;
                        } else {
                            v.UserDetailsSuccess(userEntity);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void uploadRequest(File uploadedfile, String userid) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), uploadedfile);
        builder.addFormDataPart("uploadedfile", "Atlantis.png", body);
        builder.addFormDataPart("userid", userid);
        List<MultipartBody.Part> list = builder.build().parts();
        m.upload_user(list).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseEntity entity) {
                        v.UploadUserSuccess(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
